﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using whatpricemyhouse.LINQ;

namespace whatpricemyhouse.agent
{
    public partial class SellerList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            new AdminBasicPage();
            if (User.Identity.IsAuthenticated) {
                ImageButton ImageButton1 = this.LoginView2.FindControl("ImageButton1") as ImageButton;

                ImageButton1.Attributes.Add("onmouseover", "mouseOver1(this);");
                ImageButton1.Attributes.Add("onmouseout", "mouseOut1(this);");

                ImageButton ImageButton2 = this.LoginView2.FindControl("ImageButton2") as ImageButton;

                ImageButton2.Attributes.Add("onmouseover", "mouseOver2(this);");
                ImageButton2.Attributes.Add("onmouseout", "mouseOut2(this);");
                ImageButton2.Attributes.Add("OnClick", "window.print()");

                String sellerid = Request.QueryString["sellerid"];
                String command = Request.QueryString["comm"];
                if (!String.IsNullOrEmpty(command)) {
                    if (!String.IsNullOrEmpty(sellerid)) {
                        if (command == "move") {
                            moveOld(sellerid);
                        }
                        if (command == "delete") {
                            deleteSeller(sellerid);
                        }
                    }
                }
            }
        }

        private void deleteSeller(string sellerid)
        {
            DataControllerDataContext dcdc = new DataControllerDataContext();

            //delete sell_request__loan_type_tables
            var srltts = from ss in dcdc.sell_request__loan_type_tables
                         where ss.SRTID == sellerid
                         select ss;
            foreach (var srltt in srltts)
            {
                dcdc.sell_request__loan_type_tables.DeleteOnSubmit(srltt);
            }
            //delete sell_request__living_room_tables
            var srlrts = from sss in dcdc.sell_request__living_room_tables
                         where sss.SRTID == sellerid
                         select sss;
            foreach (var srlrt in srlrts) {
                dcdc.sell_request__living_room_tables.DeleteOnSubmit(srlrt);
            }
            //out_side_area_sell_request_tables
            var osasrts = from ssss in dcdc.out_side_area_sell_request_tables
                          where ssss.SRTID == sellerid
                          select ssss;
            foreach (var osasrt in osasrts) {
                dcdc.out_side_area_sell_request_tables.DeleteOnSubmit(osasrt);
            }
            //delete dcdc.sell_request_tables
            var sellers = from s in dcdc.sell_request_tables
                          where s.SRTID == sellerid
                          select s;
            foreach (var seller in sellers) {
                dcdc.sell_request_tables.DeleteOnSubmit(seller);
                break;
            }
            
                        
            dcdc.SubmitChanges();
        }

        private void moveOld(string sellerid)
        {
            DataControllerDataContext dcdc = new DataControllerDataContext();
            var sellers = from s in dcdc.sell_request_tables
                          where s.SRTID == sellerid
                          select s;
            foreach (var seller in sellers)
            {
                seller.isOld = false;
                break;
            }
            dcdc.SubmitChanges();
        }

        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            HyperLink hl1 = e.Item.FindControl("MoveOld1") as HyperLink;
            HyperLink hl2 = e.Item.FindControl("Delete1") as HyperLink;
            bool isOld = false;
            try
            {
                if (!string.IsNullOrEmpty(Request.QueryString["old"]))
                {
                    if (Request.QueryString["old"] == "1")
                    {
                        isOld = false;
                        hl1.Visible = true;
                        hl2.Visible = false;
                    }
                    else if (Request.QueryString["old"] == "0")
                    {
                        isOld = true;
                        hl1.Visible = false;
                        hl2.Visible = true;
                    }
                }
                else {
                    isOld = false;
                    hl1.Visible = true;
                    hl2.Visible = false;
                }
            }
            catch (Exception ex) { 
                
            }
        }
    }
}